package com.ngmaster.socket;

import java.io.UnsupportedEncodingException;

/**
 * 数据缓存
 *
 */
public class ByteArray {
	/**
	 * 数组
	 */
	protected byte[] _bytes = new byte[102400];
	
	/**
	 * 读取长度
	 */
	protected int _readPos = 0;
	
	/**
	 * 写的长度
	 */
	protected int _writePos = 0;
	
	/**
	 * 初始化数组
	 */
	public void initBytes(byte[] value) {
		initBytes(value, 0, value.length);
	}
	
	/**
	 * 初始化数组
	 * @param value
	 * @param offset
	 * @param length
	 */
	public void initBytes(byte[] value, int offset, int length) {
		System.arraycopy(value, offset, _bytes, 0, value.length);
	}
	
	/**
	 * 写字符串
	 * @param value
	 */
	public void writeString(String value) {
		byte[] bytes;
		try {
			bytes = value.getBytes("UTF-8");
			writeBytes(bytes);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 写缓存
	 * @param value
	 */
	public void writeBytes(byte[] value) {
		writeInt(value.length);
		System.arraycopy(value, 0, _bytes, _writePos, value.length);
		_writePos += value.length;
	}
	
	/**
	 * 写缓存
	 * @param value
	 */
	public void writeBytes(byte[] value, int offset, int length) {
		System.arraycopy(value, offset, _bytes, _writePos, length);
		_writePos += length;
	}
	
	/**
	 * 写布尔值
	 * @param value
	 */
	public void writeBoolean(boolean value) {
		writeByte((byte)(value ? 0x01 : 0x00));
	}
	
	/**
	 * 写字节
	 * @param value
	 */
	public void writeByte(byte value) {
		System.arraycopy(BitConverter.getBytes(value), 0, _bytes, _writePos, 1);
		_writePos += 1;
	}
	
	/**
	 * 写短整形
	 * @param value
	 */
	public void writeShort(short value) {
		System.arraycopy(BitConverter.getBytes(value), 0, _bytes, _writePos, 2);
		_writePos += 2;
	}
	
	/**
	 * 写整形
	 * @param value
	 */
	public void writeInt(int value) {
		System.arraycopy(BitConverter.getBytes(value), 0, _bytes, _writePos, 4);
		_writePos += 4;
	}
	
	/**
	 * 写整形
	 * @param value
	 */
	public void writeLong(long value) {
		System.arraycopy(BitConverter.getBytes(value), 0, _bytes, _writePos, 8);
		_writePos += 8;
	}
	
	/**
	 * 写浮点数
	 * @param value
	 */
	public void writeFloat(float value) {
		System.arraycopy(BitConverter.getBytes(value), 0, _bytes, _writePos, 4);
		_writePos += 4;
	}
	
	/**
	 * 写浮点数
	 * @param value
	 */
	public void writeDouble(double value) {
		System.arraycopy(BitConverter.getBytes(value), 0, _bytes, _writePos, 8);
		_writePos += 8;
	}
	
	/**
	 * 读取布尔值
	 * @return
	 */
	public boolean readBoolean() {
		byte value = readByte();
		return (value == 0x00) ? false : true;
	}
	
	/**
	 * 读取一个字节
	 * @return
	 */
	public byte readByte() {
		byte value = _bytes[_readPos];
		_readPos += 1;
		return value;
	}
	
	/**
	 * 读取短整形
	 * @return
	 */
	public short readShort() {
		short value = BitConverter.toShort(_bytes, _readPos);
		_readPos += 2;
		return value;
	}
	
	/**
	 * 读取整形
	 * @return
	 */
	public int readInt()
	{
		int value = BitConverter.toInt(_bytes, _readPos);
		_readPos += 4;
		return value;
	}
	
	/**
	 * 读取整形
	 * @return
	 */
	public long readLong() {
		long value = BitConverter.toLong(_bytes, _readPos);
		_readPos += 8;
		return value;
	}
	
	/**
	 * 读浮点数
	 * @return
	 */
	public float readFloat() {
		float value = BitConverter.toFloat(_bytes, _readPos);
		_readPos += 4;
		return value;
	}
	
	/**
	 * 读浮点数
	 * @return
	 */
	public double readDouble() {
		double value = BitConverter.toDouble(_bytes, _readPos);
		_readPos += 8;
		return value;
	}
	
	/**
	 * 读取字符串
	 * @return
	 */
	public String readString() {
		byte[] bytes = readBytes();
		
		try {
			return new String(bytes, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return "";
	}

	/**
	 * 从指定位置读取指定字节数的字符串
	 * @param startIdx 指定读取索引
	 * @param length 要读取的字节数
	 * */
	public String readString(int startIdx, int length) {
		byte[] bytes = new byte[length];
		System.arraycopy(_bytes, startIdx, bytes, 0, length);
		_readPos += length;

		try {
			return new String(bytes, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return "";
	}
	
	/**
	 * 读取字符串
	 * @return
	 */
	public byte[] readBytes() {
		int length = readInt();
		byte[] value = new byte[length];
		System.arraycopy(_bytes, _readPos, value, 0, length);
		_readPos += length;
		return value;
	}
	
	/**
	 * 写缓存
	 * @param value
	 */
	public void readBytes(byte[] value, int offset, int length) {
		System.arraycopy(_bytes, _readPos, value, offset, length);
		_readPos += length;
	}
	
	/**
	 * 获取内存
	 * @return
	 */
	public byte[] getBytes() {
		return _bytes;
	}
	
	/**
	 * 重置
	 */
	public void reset() {
		_readPos = 0;
		_writePos = 0;
	}
	
	/**
	 * 定位并且写入
	 * @param offset
	 * @param value
	 */
	public void seekAndWrite(int offset, int value) {
		System.arraycopy(BitConverter.getBytes(value), 0, _bytes, offset, 4);
    }
	
	/**
	 * 获取写的位置
	 * @return
	 */
	public int getWritePos() {
        return _writePos;
    }
}
